const getAllClasses = () => {
    return localStorage.getItem('classData')
        ? JSON.parse(localStorage.getItem('classData'))
        : []
}
const setAllClasses = (data) => {
    localStorage.setItem('classData', JSON.stringify(data))
}

// 查询数据
export const selectClassAsync = () => {
    return new Promise((resolve) => {
        setTimeout(() => {
            const classData = getAllClasses()
            resolve({
                code: 0,
                data: classData
            })
        }, 500)
    })
}

// 修改数据
export const updateClassAsync = (data, oldData) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            let classData = getAllClasses()
            classData = classData.filter((item) => item.no !== oldData.no)
            classData.push(data)
            classData.sort(function (a, b) {
                return +a.no - +b.no
            })
            setAllClasses(classData)
            resolve({ code: 0 })
        }, 500)
    })
}

// 删除数据
export const deleteClassAsync = (no) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            let classData = getAllClasses()
            classData = classData.filter((item) => +item.no !== +no)
            setAllClasses(classData)
            resolve({ code: 0 })
        }, 500)
    })
}

// 添加数据
export const createClassAsync = (data) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            let classData = getAllClasses()
            classData.push(data)
            classData.sort(function (a, b) {
                return +a.no - +b.no
            })
            setAllClasses(classData)
            resolve({ code: 0 })
        }, 500)
    })
}
